package com.shenzhen.horselive.ui.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.BaseFragment;
import com.shenzhen.horselive.beans.CommResponseBean;
import com.shenzhen.horselive.beans.CommResponseDataBean;
import com.shenzhen.horselive.beans.UserAcountBean;
import com.shenzhen.horselive.databinding.FragmentRedGuDuiBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;
import com.shenzhen.horselive.presenter.ViewObserver;

import java.text.NumberFormat;

/**
 * 峰值兑换红包股
 */

public class RedGuDuiFragment extends BaseFragment {

    public static RedGuDuiFragment newInstance() {
        RedGuDuiFragment fragment = new RedGuDuiFragment();
        return fragment;
    }


    FragmentRedGuDuiBinding  binding;
    @Override
    protected View getContentView(LayoutInflater inflater, ViewGroup viewGroup) {
        binding =FragmentRedGuDuiBinding.inflate(inflater, viewGroup, false);
        initViews();
        initData();
        return binding.getRoot();
    }

    private void initViews() {
        binding.etNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                 String  string =  s.toString();
                 if (!TextUtils.isEmpty(string))
                 {
                     //实时计算价格
                     if (bean!=null&&bean.getData()!=null&&guRatio!=null)
                     {
                       String  fenRatio =  bean.getData().getOneRatio();
                       //兑换红包股需要的峰值个数
                       double   num = (Double.parseDouble(fenRatio)/Double.parseDouble(guRatio))*Double.parseDouble(string);
                       //需要的峰值的个数
                       binding.tvPayNum.setText(formatNumber(num+"")+"个");
                       //需要的峰值的价值
                       binding.tvPrice.setText("¥"+formatNumber(num*Double.parseDouble(bean.getData().getRatio())+""));
                     }
                 }
                 else
                 {
                     binding.tvPayNum.setText("0个");
                     //峰值价格
                     binding.tvPrice.setText("¥0.00");
                 }
            }
        });


        binding.btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });


    }

    private void submit() {
        String  num = binding.etNum.getText().toString();
        if (TextUtils.isEmpty(num))
        {
            return;
        }
//        double   targetnum = (Double.parseDouble(guRatio)/Double.parseDouble(fenRatio))*Double.parseDouble(string);
        ApiFactory.getApiService().fenDuiRedGu(User.get().getMyInfo().getToken()
        ,"1","2",num+"")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<CommResponseBean>(this) {
                    @Override
                    protected void onNextInActive(CommResponseBean commResponseBean) {
                        if (commResponseBean.isSuccess())
                        {
                            showToast("兑换成功");
                            //重新加载数据
//
                        }
                        else
                        {
                            showToast(commResponseBean.getErrMessage());
                        }
                    }
                });

    }

    private void initData() {
        //查询峰值相关的
        getAcountForType(1);
    }

    String  guRatio;
    private void getRatio() {
        ApiFactory.getApiService().ratio(User.get().getMyInfo().getToken(),"2")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<CommResponseDataBean>(this) {
                    @Override
                    protected void onNextInActive(CommResponseDataBean commResponseDataBean) {
                        if (commResponseDataBean.isSuccess())
                        {
//                           一块钱等于 0.8峰值  100个红包股
                            //红包股兑换比例
                            guRatio = commResponseDataBean.getData();
                            //峰值兑换比例
                            String   fenzhi= bean.getData().getOneRatio();
                            double  ruleer = Double.parseDouble(guRatio)/Double.parseDouble(fenzhi);

                            //一块钱的峰值可以兑换100个红包股
                            binding.tvRuler.setText("兑换规则：等值1元的蜂值可兑换"+formatNumber(guRatio+"")+"个红包股");
                        }
                    }
                });
    }
    UserAcountBean  bean;
    //帐户类型: 0:人民币帐户 ,1:蜂值帐户 ,2:红包股帐户 ,3:金卡帐户
    private void getAcountForType(int i) {
        ApiFactory.getApiService().accountTypes(User.get().getMyInfo().getToken(),i+"")
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<UserAcountBean>(this) {
                    @Override
                    protected void onNextInActive(UserAcountBean userAcountBean) {
                       if (userAcountBean.isSuccess())
                       {
                           bean =userAcountBean;
//                           //峰值价值
//                           binding.tvFenzhiValue.setText("¥"+userAcountBean.getData().getValue());
//                           //峰值个数
//                           binding.tvFenzhiNum.setText(userAcountBean.getData().getAmount()+"");
//                           //兑换人命币的价格
//                           binding.tvFenzhiPrice.setText("¥"+userAcountBean.getData().getRatio());
                           //账户剩余的峰值个数
                           binding.tvFenzhuGeshu.setText(formatNumber(userAcountBean.getData().getAmount())+"个");
                           //查询兑换比例红包股
                           getRatio();

                       }
                    }
                });

    }


    @Override
    public void initImmersionBar() {

    }


    private  String  formatNumber(String  num)
    {
        NumberFormat  format = NumberFormat.getInstance();
        format.setMaximumFractionDigits(10);
        return format.format(Double.parseDouble(num));

    }



}